const tool = require("../tools/tool");
const db = require("../db");
// const patrol = require("../methods/patrol");
// const campuses = require("./campuses");
// const categories = require("./categories");
// const settings = require("./settings");

module.exports = {
    async getUserPermissionsListType1(userId) {
        let t = await db.selectWithParams(`select * from permissions where userId = ? and type = 1`, [userId])
        t = await tool.array.format(t, async (item) => {
            item.createdAt_read = tool.formatTimeNew(new Date(item.createdAt))
            item.campusInfo = await tool.methods.campuses().getInfo(item.campusId)
            item.categoryInfo = await tool.methods.categories().getInfo(item.categoryId)
            return item
        })
        return t
    },
    async getUserPermissionsListType2(userId) {
        let t = await db.selectWithParams(`select * from permissions where userId = ? and type = 2`, [userId])
        t = await tool.array.format(t, async (item) => {
            item.createdAt_read = tool.formatTimeNew(new Date(item.createdAt))
            item.campusInfo = await tool.methods.campuses().getInfo(item.campusId)
            item.categoryInfo = await tool.methods.categories().getInfo(item.categoryId)
            return item
        })
        return t
    },
    async checkUserPermission(userId, type, campusId, categoryId, withoutAdmin = false) {
        const user = require("../methods/user");
        let userData = await user.getSingleUserInfoByUserId(userId)
        if (userData.isAdmin && !withoutAdmin) return true
        if (type == 2 && !(await tool.methods.settings().getItem('progress_review'))) return false
        return (await db.selectWithParams(`select * from permissions where userId = ? and type = ? and campusId = ? and categoryId = ?`, [userId, type, campusId, categoryId])).length != 0
    },
    async getUserPatrolPermissions(userId, patrolId) {
        const user = require("../methods/user");
        let patrolInfo = await tool.methods.patrol().getHead(patrolId)
        let userData = await user.getSingleUserInfoByUserId(userId)
        return {
            handle: ((await this.checkUserPermission(userId, 1, patrolInfo.campusId, patrolInfo.categoryId)) && (patrolInfo.status == 1 || patrolInfo.status == 2)),
            review: ((await this.checkUserPermission(userId, 2, patrolInfo.campusId, patrolInfo.categoryId)) && (patrolInfo.status == 3 || patrolInfo.status == 4 || patrolInfo.status == 5)) && (await tool.methods.settings().getItem('progress_review')),
            delete: (userData && userData.isAdmin) || (userId == patrolInfo.userId && patrolInfo.status == 1),
            feedBack: (userId == patrolInfo.userId && patrolInfo.status == 3),
            delay: ((await this.checkUserPermission(userId, 1, patrolInfo.campusId, patrolInfo.categoryId)) && (patrolInfo.status == 1 || patrolInfo.status == 2)) && !patrolInfo.endTime3,
            priority: patrolInfo.status < 3 && ((userData && patrolInfo.userId == userId) || (userData && userData.isAdmin)),
            edit: tool.object.deleteUndefinedKeys({
                campusCategory: ((await this.checkUserPermission(userId, 1, patrolInfo.campusId, patrolInfo.categoryId)) && (patrolInfo.status == 1)),
            })
        }
    },
    async isUserCanDo(userId, patrolId, type) {
        let patrolInfo = await tool.methods.patrol().getHead(patrolId)
        if (type === 1) {
            if (patrolInfo.status == 1 || patrolInfo.status == 2) {
                if ((await this.checkUserPermission(userId, 1, patrolInfo.campusId, patrolInfo.categoryId))) {
                    return true
                } else {
                    throw '无权处理'
                }
            } else {
                throw '巡查不在可处理阶段'
            }
        }
        if (type === 2) {
            if (patrolInfo.status == 3 || patrolInfo.status == 4 || patrolInfo.status == 5) {
                if ((await this.checkUserPermission(userId, 2, patrolInfo.campusId, patrolInfo.categoryId))) {
                    return true
                } else {
                    throw '无权驳回'
                }
            } else {
                throw '巡查不在可驳回阶段'
            }
        }
        if (type === 3) {
            if (patrolInfo.status === 3) {
                if (patrolInfo.userId == userId) {
                    return true
                } else {
                    throw '无权评价'
                }
            } else {
                throw '巡查不在可评价阶段'
            }
        }
        throw '操作类型错误'
    },
    async add(userId, type, campusId, categoryId) {
        let re = await db.selectWithParams(`select * from permissions where userId = ? and type = ? and campusId = ? and categoryId = ?`, [userId, type, campusId, categoryId])
        if (re.length) throw '权限已存在'
        if (!campusId) throw '校区ID有误'
        if (!categoryId) throw '类别ID有误'
        await db.insertInto('permissions', {
            userId,
            type,
            campusId,
            categoryId
        })
    },
    async delete(perId) {
        await db.deleteById('permissions', perId)
    },
    async deleteUserAllPers(userId) {
        await db.exeWithParams(`delete from permissions where userId = ?`, [userId])
    }
}